package cn.hahah.module.blog.controller.admin.visithistorybrowse;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.service.article.ArticleService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.visithistorybrowse.vo.*;
import cn.hahah.module.blog.dal.dataobject.visithistorybrowse.VisitHistoryBrowseDO;
import cn.hahah.module.blog.service.visithistorybrowse.VisitHistoryBrowseService;

@Tag(name = "管理后台 - 游客文章浏览记录")
@RestController
@RequestMapping("/blog/visit-history-browse")
@Validated
public class VisitHistoryBrowseController {

    @Resource
    private VisitHistoryBrowseService visitHistoryBrowseService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建游客文章浏览记录")
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:create')")
    public CommonResult<Long> createVisitHistoryBrowse(@Valid @RequestBody VisitHistoryBrowseSaveReqVO createReqVO) {
        return success(visitHistoryBrowseService.createVisitHistoryBrowse(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新游客文章浏览记录")
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:update')")
    public CommonResult<Boolean> updateVisitHistoryBrowse(@Valid @RequestBody VisitHistoryBrowseSaveReqVO updateReqVO) {
        visitHistoryBrowseService.updateVisitHistoryBrowse(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除游客文章浏览记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:delete')")
    public CommonResult<Boolean> deleteVisitHistoryBrowse(@RequestParam("id") Long id) {
        visitHistoryBrowseService.deleteVisitHistoryBrowse(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得游客文章浏览记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:query')")
    public CommonResult<VisitHistoryBrowseRespVO> getVisitHistoryBrowse(@RequestParam("id") Long id) {
        VisitHistoryBrowseDO visitHistoryBrowse = visitHistoryBrowseService.getVisitHistoryBrowse(id);
        return success(BeanUtils.toBean(visitHistoryBrowse, VisitHistoryBrowseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得游客文章浏览记录分页")
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:query')")
    public CommonResult<PageResult<VisitHistoryBrowseRespVO>> getVisitHistoryBrowsePage(@Valid VisitHistoryBrowsePageReqVO pageReqVO) {
        PageResult<VisitHistoryBrowseDO> pageResult = visitHistoryBrowseService.getVisitHistoryBrowsePage(pageReqVO);
        PageResult<VisitHistoryBrowseRespVO> result = BeanUtils.toBean(pageResult, VisitHistoryBrowseRespVO.class);
        for (VisitHistoryBrowseRespVO visitHistoryBrowseRespVO : result.getList()) {
            ArticleDO articleDO = articleService.getArticle(visitHistoryBrowseRespVO.getArticleId());

            if( articleDO != null){
                visitHistoryBrowseRespVO.setArticleName(articleDO.getTitle());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出游客文章浏览记录 Excel")
    @PreAuthorize("@ss.hasPermission('blog:visit-history-browse:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportVisitHistoryBrowseExcel(@Valid VisitHistoryBrowsePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<VisitHistoryBrowseDO> list = visitHistoryBrowseService.getVisitHistoryBrowsePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "游客文章浏览记录.xls", "数据", VisitHistoryBrowseRespVO.class,
                        BeanUtils.toBean(list, VisitHistoryBrowseRespVO.class));
    }

}